package tq.com.tqteacher.activity.newPublish;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;

import org.json.JSONArray;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.TQCropPictureActivity;
import tq.com.tqteacher.activity.TQNewPublishFXJActivity;
import tq.com.tqteacher.model.TQNewPingJiangPublish;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQBasePublishInfoView;
import tq.com.tqteacher.view.TQHeader0ViewHold;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.tqteacher.view.viewhold.TQPublishResultViewHelper;
import tq.com.tqteacher.view.viewhold.TQPublishSaveButtonViewHold;

public class TQJiangPingResultActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    private TQPublishResultViewHelper mPublishResultViewHelper;
    private TQPublishSaveButtonViewHold mTQPublishSaveButtonViewHold;

    private TextView mPictureView;
    private TextView mVideoView;

    static public void show(Activity pActivity){
        Intent intent = new Intent(pActivity, TQJiangPingResultActivity.class);
        pActivity.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBar();
        setContentView(R.layout.activity_jiangpinpublish_result_layout);
        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("评讲");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQJiangPingResultActivity.this.finish();
            }
        });

        if(mPublishResultViewHelper == null){
            mPublishResultViewHelper = new TQPublishResultViewHelper(findViewById(R.id.baseInfo));
            mPublishResultViewHelper.setModifyClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent lIntent = new Intent(TQJiangPingResultActivity.this, TQNewPublishFXJActivity.class);
                    startActivity(lIntent);
                }
            });
        }
        if(mTQPublishSaveButtonViewHold == null){
            mTQPublishSaveButtonViewHold = new TQPublishSaveButtonViewHold(findViewById(R.id.bottom));
            mTQPublishSaveButtonViewHold.setPublishClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublish();
                }
            });
        }

        mPictureView = findViewById(R.id.pictureCount);
        mVideoView = findViewById(R.id.videoCount);
        findViewById(R.id.video_area).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoCaptureView();
            }
        });

        findViewById(R.id.picture_area).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoCropView();
            }
        });

        TQNewPingJiangPublish lJiangPublish = (TQNewPingJiangPublish)TQPublishType.getSharePublishItem();
        if(!TQPublishType.getSharePublishItem().isModify()){
            TQCropPictureActivity.show(this);
        }
        if(lJiangPublish.isPublish()){
            mTQPublishSaveButtonViewHold.setAlreadyPublish();
        }

        if(TQPublishType.getSharePublishItem().getNeedloadRemote()){
//            getDetail();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        bindData();
    }

    private void bindData(){
        mPublishResultViewHelper.setTBResult(TQPublishType.getSharePublishItem());

        TQNewPingJiangPublish lJiangPublish = (TQNewPingJiangPublish)TQPublishType.getSharePublishItem();
        mPictureView.setText(String.format("截取(%d)", lJiangPublish.getCropCount()));
        mVideoView.setText(String.format("讲评(%d)", lJiangPublish.getCaptureCount()));

        lJiangPublish.setModify(true);
    }

    private void getDetail(){
//        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
//            @Override
//            public void result(TQNetResult pNetResult) {
//                if(!pNetResult.isSuccess()){
//                    AlertDialogHelper.show(TQJiangPingResultActivity.this, pNetResult.getErrMsg());
//                    return;
//                }
//
//                try {
//                    if(pNetResult.getData() == null)return;
//
//                    JSONObject lObject = pNetResult.getData().optJSONObject("material");
//                    if(lObject == null) return;
//
//                    JSONObject lBaseInfo = lObject.optJSONObject("material");
//                    if(lBaseInfo != null){
//                        TQPublishType.getSharePublishItem().fromJson(lBaseInfo);
//                    }
//
//                    JSONArray lAttarch = lObject.optJSONArray("attachment");
//                    TQPublishType.getSharePublishItem().refreshAttach(lAttarch);
//
//                    bindData();
//                }finally {
//                    TQLoadingHelper.dimiss();
//                }
//            }
//        });

        TQPublishType.getSharePublishItem().getDetailFromServer(this, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                bindData();
            }
        });
    }

    private void gotoCropView(){
        TQCropPictureActivity.show(this);
    }
    private void gotoCaptureView(){
        TQVideoSetsManager.show(this);
    }

    private void onPublish(){
        if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.optimize){
            AlertDialogHelper.show(this, "已同步更新");
        }else {
            TQNewPingJiangPublish lTQNewPingJiangPublish = (TQNewPingJiangPublish)TQPublishType.getSharePublishItem();
            if(lTQNewPingJiangPublish.getAtachModels() == null ||
                    lTQNewPingJiangPublish.getAtachModels().size() == 0){
                AlertDialogHelper.show(this, "评讲内容不能为空");
                return;
            }

            TQPublishType.getSharePublishItem().onPublish(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQJiangPingResultActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().setState(TQSystemDefine.PUBLISH_STATE_Publish);
                    mTQPublishSaveButtonViewHold.setAlreadyPublish();
                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));
                    TQJiangPingResultActivity.this.finish();
                }
            });
        }

    }
}
